package org.jeecg.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.entity.DbIndustry;
import org.jeecg.modules.mapper.DbIndustryMapper;
import org.jeecg.modules.service.IDbIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: db_industry
 * @Author: jeecg-boot
 * @Date:   2024-06-17
 * @Version: V1.0
 */
@Service
public class DbIndustryServiceImpl extends ServiceImpl<DbIndustryMapper, DbIndustry> implements IDbIndustryService {

    @Autowired
    private DbIndustryMapper dbIndustryMapper;
    /**
     * 行业二级查询
     * @return
     */
    public List<DbIndustry> getIndustry() {
        List<DbIndustry> json=dbIndustryMapper.selectList(new QueryWrapper<DbIndustry>().eq("lev",1));
        if(json.isEmpty()){
            return json;
        }
        List list = new ArrayList<>();

        json.forEach(item->{
            list.add(item.getCode());
        });
        List<DbIndustry> json2 = dbIndustryMapper.selectList(new QueryWrapper<DbIndustry>().eq("lev", 2).in("parent_code", list));
        Map<String,List<DbIndustry>> mdv=json2.stream().collect(Collectors.groupingBy(DbIndustry::getParentCode));
        json.stream().forEach(item->item.setChildren(mdv.get(item.getCode())));
        return json;
    }
}
